package com.imageim.yimirror.ui.widght;

import android.app.Activity;
import android.content.Context;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;

import com.imageim.yimirror.R;
import com.imageim.yimirror.common.ui.SceneManager;
import com.imageim.yimirror.common.ui.btn.BtnCircleClose;
import com.imageim.yimirror.ui.activity.MainActivity;
import com.imageim.yimirror.ui.activity.ScanFaceActivity;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import butterknife.Unbinder;

/**
 * Created by Yuaihen.
 * on 2018/11/29
 * 身线图和雷达图界面共同的Title
 */
public class BodyAndRadarTitleView extends LinearLayout {

    @BindView(R.id.ib_look_cloth)
    ImageView mIbLookCloth;
    @BindView(R.id.btn_circle_close)
    BtnCircleClose mBtnCircleClose;
    @BindView(R.id.ib_restart_test)
    ImageView mIbRestartTest;
    private Unbinder mBind;
    private Context mContext;

    public BodyAndRadarTitleView(@NonNull Context context) {
        super(context);
        initView(context);
    }

    public BodyAndRadarTitleView(@NonNull Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        initView(context);
    }

    private void initView(Context context) {
        mContext = context;
        View view = LayoutInflater.from(context).inflate(R.layout.view_body_and_radar_title, this);
        mBind = ButterKnife.bind(this, view);

        initListener();
    }

    private void initListener() {
        mBtnCircleClose.setCloseImage(0, 300);
        mBtnCircleClose.setOnTimeOverListener(this::finishActivity);
    }


    public void restartTime(int time) {
        mBtnCircleClose.restart(time);
    }

    public void stopTime() {
        if (mBtnCircleClose != null) {
            mBtnCircleClose.destory();
        }
    }

    @Override
    protected void onDetachedFromWindow() {
        super.onDetachedFromWindow();
        if (mBtnCircleClose != null) {
            mBtnCircleClose.destory();
        }
        if (mBind != null) {
            mBind.unbind();
        }

    }

    private void finishActivity() {
        Activity activity = (Activity) mContext;
        if (activity != null && !activity.isFinishing()) {
            activity.finish();
        }
    }

    @OnClick({R.id.ib_look_cloth, R.id.ib_restart_test})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.ib_look_cloth:
                //查看服饰  跳转到为您推荐界面
                SceneManager.toScene(mContext, MainActivity.class, null);
                finishActivity();
                break;
            case R.id.ib_restart_test:
                //重新测试 跳转拍照界面
                SceneManager.toScene(mContext, ScanFaceActivity.class, null);
                finishActivity();
                break;
            default:
                break;
        }
    }
}
